package com.jbob.collection;

import java.text.Normalizer;
import java.util.ArrayList;

import org.tartarus.snowball.SnowballStemmer;
import org.tartarus.snowball.ext.frenchStemmer;

import com.jbob.parser.ParserHelper;

public class Query 
{
	private String mId;
	private String mText;
	private String mNarrative;
	
	private ArrayList<String> wordsToSeek;
	private ArrayList<String> synonymsToSeek;
	private ArrayList<String> parentsToSeek;
	private ArrayList<String> subClassToSeek;
	// private ArrayList<String> individualsToSeek;
	
	public Query(String id, String text, String narrative)
	{
		setmId(id);
		setmNarrative(narrative);
		wordsToSeek = new ArrayList<String>();
		synonymsToSeek = new ArrayList<String>();
		parentsToSeek = new ArrayList<String>();
		subClassToSeek = new ArrayList<String>();
		setmText(text);
	}

	public String getmId()
	{
		return mId;
	}

	public void setmId(String mId)
	{
		this.mId = mId;
	}

	public String getmText() 
	{
		return mText;
	}
	
	@SuppressWarnings("unused")
	private boolean alreadyContains(String str)
	{
		return (wordsToSeek.contains(str)||
				synonymsToSeek.contains(str)||
				parentsToSeek.contains(str)||
				subClassToSeek.contains(str));
	}

	public void setmText(String text) 
	{
		this.mText = text;
		
		SnowballStemmer stemmer = new frenchStemmer();
		
		text = text.toLowerCase();
		// retrait de toute la ponctuation et des guillemets anglais
		text = text.replaceAll("\\p{Punct}|«|»", " ");
		// Split sur tout espace qu'il s'agisse d'une tab, d'un espace simple, d'un saut de ligne ou autre
		text = text.trim();
		String[] content = text.split("\\p{Space}");
		
		for(int i = 0 ; i < content.length ; i++)
		{
			String str = content[i];
			if(str != null && !str.isEmpty() && !ParserHelper.getStopList().contains(str))
			{
				stemmer.setCurrent(str);
				stemmer.stem();
				String stemmedString = stemmer.getCurrent();
				stemmedString = str; //TODO
				wordsToSeek.add(stemmedString);
				//System.out.println(stemmedString);
			}
		}
	}
	
	public void addParentToSeek(String parent)
	{
		if(alreadyContains(parent))
		{
			return;
		}
		
		parentsToSeek.add(parent);
	}
	
	public void addSubClassToSeek(String subClass)
	{
		if(alreadyContains(subClass))
		{
			return;
		}
		
		subClassToSeek.add(subClass);
	}
	
	public ArrayList<String> getParentsToSeek()
	{
		return parentsToSeek;
	}
	
	public ArrayList<String> getSubClassToSeek()
	{
		return subClassToSeek;
	}
	
	public void addSysnonymToSeek(String synonym)
	{
		if(alreadyContains(synonym))
		{
			return;
		}
		
		synonymsToSeek.add(synonym);
	}
	
	public void stemAllContents()
	{
		SnowballStemmer stemmer = new frenchStemmer();
		
		for(int i = 0 ; i < wordsToSeek.size() ; i++) 
		{
			String text = wordsToSeek.get(i);
			if(text != null && !text.isEmpty())
			{
				stemmer.setCurrent(text);
				stemmer.stem();
				String stemmedString = stemmer.getCurrent();
				stemmedString = Normalizer.normalize(stemmedString, Normalizer.Form.NFD);
				stemmedString = stemmedString.replaceAll("[^\\p{ASCII}]", "");
				stemmedString = stemmedString.toLowerCase();
				wordsToSeek.set(i, stemmedString);
			}
		}
		
		for(int i = 0 ; i < synonymsToSeek.size() ; i++) 
		{
			String text = synonymsToSeek.get(i);
			if(text != null && !text.isEmpty())
			{
				stemmer.setCurrent(text);
				stemmer.stem();
				String stemmedString = stemmer.getCurrent();
				stemmedString = Normalizer.normalize(stemmedString, Normalizer.Form.NFD);
				stemmedString = stemmedString.replaceAll("[^\\p{ASCII}]", "");
				stemmedString = stemmedString.toLowerCase();
				synonymsToSeek.set(i, stemmedString);
			}
		}
		
		for(int i = 0 ; i < parentsToSeek.size() ; i++) 
		{
			String text = parentsToSeek.get(i);
			if(text != null && !text.isEmpty())
			{
				stemmer.setCurrent(text);
				stemmer.stem();
				String stemmedString = stemmer.getCurrent();
				stemmedString = Normalizer.normalize(stemmedString, Normalizer.Form.NFD);
				stemmedString = stemmedString.replaceAll("[^\\p{ASCII}]", "");
				stemmedString = stemmedString.toLowerCase();
				parentsToSeek.set(i, stemmedString);
			}
		}
		
		for(int i = 0 ; i < subClassToSeek.size() ; i++) 
		{
			String text = subClassToSeek.get(i);
			if(text != null && !text.isEmpty())
			{
				stemmer.setCurrent(text);
				stemmer.stem();
				String stemmedString = stemmer.getCurrent();
				stemmedString = Normalizer.normalize(stemmedString, Normalizer.Form.NFD);
				stemmedString = stemmedString.replaceAll("[^\\p{ASCII}]", "");
				stemmedString = stemmedString.toLowerCase();
				subClassToSeek.set(i, stemmedString);
			}
		}
	}
	
	public ArrayList<String> getSynonymsToSeek()
	{
		return synonymsToSeek;
	}

	public String getmNarrative() 
	{
		return mNarrative;
	}

	public void setmNarrative(String mNarrative) 
	{
		this.mNarrative = mNarrative;
	}

	public ArrayList<String> getWordsToSeek()
	{
		return wordsToSeek;
	}
}
